/*
 * Created on Jul 5, 2005 TODO To change the template for this generated file go
 * to Window - Preferences - Java - Code Style - Code Templates
 */
package com.novix.utility.web;

import javax.servlet.http.Cookie;

/**
 * @author Dev TODO To change the template for this generated type comment go to
 *         Window - Preferences - Java - Code Style - Code Templates
 */
public class CookieLoader {

    /**
     * 
     */
    private CookieLoader() {
        super();      
    }

    /**
     * Given an array of Cookies, a name, and a default value, this method tries
     * to find the value of the cookie with the given name. If there is no
     * cookie matching the name in the array, then the default value is returned
     * instead.
     */

    public static String getCookieValue(Cookie[] cookies, String cookieName, String defaultValue) {
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                if (cookieName.equals(cookie.getName()))
                    return (cookie.getValue());
            }
        }
        return (defaultValue);
    }

    /**
     * Given an array of cookies and a name, this method tries to find and
     * return the cookie from the array that has the given name. If there is no
     * cookie matching the name in the array, null is returned.
     */

    public static Cookie getCookie(Cookie[] cookies, String cookieName) {
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                if (cookieName.equals(cookie.getName()))
                    return (cookie);
            }
        }
        return (null);
    }
}
